/*
 * interface.c
 *
 *  Created on: 2013-5-14
 *      Author: xkaying
 */


#include "interface.h"
#include "template.h"
#include "../common/list.h"

#include <assert.h>
#include <stdlib.h>

struct _interface{
		int prority;
		char *name;
		Template *ptemplate;
		List *pitems;//函数列表定义
		List *super;//父接口列表
};

Interface *InterfaceInit(int prority)
{
	Interface *pface;
	pface=(Interface*)malloc(sizeof(Interface));
	pface->prority=prority;
	pface->name=0;
	pface->ptemplate=0;
	pface->pitems=0;
	pface->super=0;
	return pface;
}

void InterfaceAppendName(Interface *pface,char *name)
{
	assert(pface);
	assert(name);
	pface->name=name;
}

void InterfaceAppendTemplate(Interface *pface,Template *ptmp)
{
	assert(pface);
	assert(ptmp);
	pface->ptemplate=ptmp;
}

void InterfaceAppendFunction(Interface *pface,Function *pfun)
{
	assert(pface);
	assert(pfun);
	if(pface->pitems==0){
		pface->pitems=ListInit();
	}
	ListCursor *pcursor;
	pcursor=ListGetCursor(pface->pitems);
	Function *ptfun;
	ptfun=ListForeach(pface->pitems,pcursor);
	while(ptfun){
		if(FunctionNameCompare(pfun,ptfun)){
			printf("警告:接口%s中,函数%s命名冲突.后定义的接口函数自动忽略.\n",pface->name,FunctionName(pfun));
			return;
		}
		ptfun=ListForeach(pface->pitems,pcursor);
	}
	ListPush(pface->pitems,pfun);
}

int InterfaceIsEmpty(Interface *pface)
{
	assert(pface);
	if(pface->pitems){
		return 0;
	}
	return 1;
}

void InterfaceAppendSuper(Interface *pface,char *super)
{
	assert(pface);
	assert(super);
	if(pface->super==0){
		pface->super=ListInit();
	}
	ListPush(pface->super,super);
}
